
import { axios } from '@/utils/request'
import { METHOD } from '@/store/mutation-types'
import storage from 'store'
import { UI_CACHE_DB_DICT_DATA } from '@/store/mutation-types'

const basicUrl = '/sys/dict/'

// 新增字典
export function addDict(parameter) {
  return axios({
    url: `${basicUrl}/add`,
    method: METHOD.POST,
    data: parameter
  })
}

// 更新字典
export function editDict(parameter) {
  return axios({
    url: `${basicUrl}/edit`,
    method: METHOD.PUT,
    data: parameter
  })
}

// 新增字典项
export function addDictItem(code, parameter) {
  return axios({
    url: `${basicUrl}${code}/add`,
    method: METHOD.POST,
    data: parameter
  })
}

// 更新字典项
export function editDictItem(code, parameter) {
  return axios({
    url: `${basicUrl}${code}/edit`,
    method: METHOD.PUT,
    data: parameter
  })
}

// 删除字典项
export function deleteDictItem(url, parameter) {
  return axios({
    url: url,
    method: METHOD.DELETE,
    params: parameter
  })
}

// 字典标签专用（通过code获取字典数组）
export function ajaxGetDictItems(code) {
  return axios({
    url: `${basicUrl}tree/${code}`,
    method: METHOD.GET
  })
}

// 从缓存中获取字典配置
export function getDictItemsFromCache(dictCode) {
  if (storage.get(UI_CACHE_DB_DICT_DATA) && storage.get(UI_CACHE_DB_DICT_DATA)[dictCode]) {
    const dictItems = storage.get(UI_CACHE_DB_DICT_DATA)[dictCode]
    return dictItems
  }
}
